using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Ideopuzzle.Resources;

namespace Ideopuzzle
{
    public enum Difficulty
    { 
        Easy,
        Medium,
        Hard,
        VeryHard
    }

    public class Options : Menu
    {
        #region Fields

        SpriteFont font36;
        AudioManager audio_manager;
        public static Difficulty DifficultyLevel = Difficulty.Medium;
        public static float Volume = 0.5f;
        public static bool Sound = true;
        public static bool PlayBackgroundMusic = false;

        #endregion

        public Options(Game game)
            : base(game)
        { }

        public override void Initialize()
        {
            Game.Components.Add(new InputManager(Game));
            input_manager = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            input_manager.OnTap += new InputManager.Tap(OnTap);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            audio_manager = Game.Services.GetService(typeof(AudioManager)) as AudioManager;
            font36 = Game.Content.Load<SpriteFont>("Fonts/Light36");

            CreateButtonsList();
            LoadRenderableObjects();

            base.LoadContent();
        }

        protected override void Dispose(bool disposing)
        {
            if (input_manager != null)
                input_manager.OnPress -= new InputManager.Tap(OnTap);
            Game.Services.RemoveService(typeof(IInputManager));

            base.Dispose(disposing);
        }

        public override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back))
            {
                ScreenManager.SelectMainMenu(Game); 
                audio_manager.PlayClick();
            }
                
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            base.Draw(gameTime);

            sprite_batch.Begin();

            var diff_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f + 30, 248);
            switch (DifficultyLevel)
            {
                case Difficulty.Easy: sprite_batch.DrawString(font36, Lang.O_Easy, diff_pos, new Color(139, 192, 63)); break;
                case Difficulty.Medium: sprite_batch.DrawString(font36, Lang.O_Medium, diff_pos, new Color(230, 218, 30)); break;
                case Difficulty.Hard: sprite_batch.DrawString(font36, Lang.O_Hard, diff_pos, new Color(241, 151, 32)); break;
                case Difficulty.VeryHard: sprite_batch.DrawString(font36, Lang.O_VHard, diff_pos, new Color(230, 30, 38)); break;
            }

            var vol_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f + 30, 194);
            if (Volume == .25f) sprite_batch.DrawString(font36, "I", vol_pos, new Color(139, 192, 63));
            else if (Volume == .5f) sprite_batch.DrawString(font36, "II", vol_pos, new Color(139, 192, 63));
            else if (Volume == .75f) sprite_batch.DrawString(font36, "III", vol_pos, new Color(139, 192, 63));
            else if (Volume == 1) sprite_batch.DrawString(font36, "IIII", vol_pos, new Color(139, 192, 63));

            var soun_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f + 30, 140);
            if (Sound) sprite_batch.DrawString(font36, Lang.O_On, soun_pos, new Color(139, 192, 63));
            else sprite_batch.DrawString(font36, Lang.O_Off, soun_pos, new Color(230, 30, 38));

            var music_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f + 30, 302);
            if (PlayBackgroundMusic) sprite_batch.DrawString(font36, Lang.O_On, music_pos, new Color(139, 192, 63));
            else sprite_batch.DrawString(font36, Lang.O_Off, music_pos, new Color(230, 30, 38));

            sprite_batch.End();
        }

        #region Private Methods

        private void CreateButtonsList()
        {
            var mw = GraphicsDevice.Viewport.Width / 2.0f;

            entries = new List<MenuEntry>();

            var e = new TextureButton(sprite_batch, Game.Content.Load<Texture2D>("Textures/Shared/menuback"),
                new Vector2(GraphicsDevice.Viewport.Width / 2.0f - Game.Content.Load<Texture2D>("Textures/Shared/menuback").Width / 2.0f, 390));

            e.OnPress += new Press(GetButtonEventHandler(0));

            entries.Add(e);

            var sm = font36.MeasureString(Lang.O_Sound);
            var lb = new LabelButton(sprite_batch, font36, new Vector2(mw - sm.X + 30, 140), Color.Transparent, "AAAAAAAAAAAAA");
            lb.OnPress += new Press(GetButtonEventHandler(1));

            entries.Add(lb);

            sm = font36.MeasureString(Lang.O_Volume);
            lb = new LabelButton(sprite_batch, font36, new Vector2(mw - sm.X + 30, 194), Color.Transparent, "AAAAAAAAAAAAA");
            lb.OnPress += new Press(GetButtonEventHandler(2));

            entries.Add(lb);

            sm = font36.MeasureString(Lang.O_Difficulty);
            lb = new LabelButton(sprite_batch, font36, new Vector2(mw - sm.X + 30, 248), Color.Transparent, "AAAAAAAAAAAAA");
            lb.OnPress += new Press(GetButtonEventHandler(3));

            entries.Add(lb);

            sm = font36.MeasureString(Lang.O_BGMusic);
            lb = new LabelButton(sprite_batch, font36, new Vector2(mw - sm.X + 30, 302), Color.Transparent, "AAAAAAAAAAAAA");
            lb.OnPress += new Press(GetButtonEventHandler(4));

            entries.Add(lb);
        }

        private void LoadRenderableObjects()
        {
            renderables = new List<RenderableObject>();

            renderables.Add(new ImgObject(sprite_batch, Game.Content.Load<Texture2D>("Textures/MainMenu/menubackground"),
                Vector2.Zero));

            var sm = font36.MeasureString(Lang.O_Sound);
            var mw = GraphicsDevice.Viewport.Width / 2.0f;
            renderables.Add(new TextObject(sprite_batch, font36, Lang.O_Sound, new Vector2(mw - sm.X, 140), Color.White));
            sm = font36.MeasureString(Lang.O_Volume);
            renderables.Add(new TextObject(sprite_batch, font36, Lang.O_Volume, new Vector2(mw - sm.X, 194), Color.White));
            sm = font36.MeasureString(Lang.O_Difficulty);
            renderables.Add(new TextObject(sprite_batch, font36, Lang.O_Difficulty, new Vector2(mw - sm.X, 248), Color.White));
            sm = font36.MeasureString(Lang.O_BGMusic);
            renderables.Add(new TextObject(sprite_batch, font36, Lang.O_BGMusic, new Vector2(mw - sm.X, 302), Color.White));
        }

        private Action<MenuEntry> GetButtonEventHandler(int index)
        {
            switch (index)
            {
                case 0:
                    return (MenuEntry m) => { ScreenManager.SelectMainMenu(Game); audio_manager.PlayClick(); };
                case 3:
                    return (MenuEntry m) => { DifficultyLevel = (Difficulty)(((int)DifficultyLevel + 1) % 4); audio_manager.PlayClick(); };
                case 2:
                    return (MenuEntry m) => { Volume += 0.25f; if (Volume > 1.0f) Volume = 0.0f; MediaPlayer.Volume = Volume; audio_manager.PlayClick(); };
                case 1:
                    return (MenuEntry m) => { Sound = !Sound; audio_manager.PlayClick(); };
                case 4:
                    return (MenuEntry m) => { PlayBackgroundMusic = !PlayBackgroundMusic; audio_manager.PlayClick(); };
                default:
                    return (MenuEntry m) => { Game.Exit(); audio_manager.PlayClick(); };
            }
        }

        #endregion
    }
}
